package com.ley.yikang.controller;

import com.ley.yikang.Annotation.PassToken;
import com.ley.yikang.Annotation.UserLoginToken;
import com.ley.yikang.entity.CheckTask;
import com.ley.yikang.entity.Doctor;
import com.ley.yikang.entity.Elder;
import com.ley.yikang.service.CheckTaskService;
import com.ley.yikang.service.DoctorService;
import com.ley.yikang.service.ElderService;
import com.ley.yikang.util.ResultData;
import com.ley.yikang.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/doctor")
public class DoctorController {
    private final DoctorService doctorService;
    private final CheckTaskService checkTaskService;
    private final ElderService elderService;

    @Autowired
    public DoctorController(DoctorService doctorService, CheckTaskService checkTaskService, ElderService elderService) {
        this.doctorService = doctorService;
        this.checkTaskService = checkTaskService;
        this.elderService = elderService;
    }

    @PassToken
    @PostMapping("/register")
    public ResultData<Boolean> addUser(@RequestBody Doctor doctor) {
        //判断手机号是否合法
        if (!doctor.getPhone().matches("1(3[0-9]|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\\d{8}")) {
            System.out.println("手机号不合法！");
            return ResultData.fail("手机号不合法！");
        }
        //判断用户是否存在
        Doctor doctor1 = doctorService.findDoctor(doctor.getPhone());
        if (doctor1 == null) {
            doctorService.addDoctor(doctor);
            System.out.println("注册医生用户成功！");
            return ResultData.success(true, "用户注册成功！");
        } else {
            System.out.println("注册医生用户失败,该手机号已被使用！");
            return ResultData.fail("用户注册失败,该手机号已被使用！");
        }
    }

    @PassToken
    @PostMapping(value = "/login")
    public ResultData<String> login(@RequestBody Doctor doctor) {
        Doctor doctor1 = doctorService.findDoctor(doctor.getPhone());
        if (doctor1 != null) {
            //系统中有这样的用户
            if (doctor1.getPassword().equals(doctor.getPassword())) {
                System.out.println("找到医生用户！登陆成功！");
                //返回token
                return ResultData.success(TokenUtil.getDoctorToken(doctor1), "用户登陆成功！");
            } else {
                System.out.println("密码错误！登陆失败！");
                return ResultData.fail("密码错误！登陆失败！");
            }
        } else
            return ResultData.fail("用户名或密码错误！");
    }

    @UserLoginToken
    @DeleteMapping("/delDoctor")
    public ResultData<Boolean> delete(@RequestBody Doctor doctor) {
        //判断医生是否存在
        if (doctorService.findDoctorById(doctor.getId()) == null) {
            System.out.println("没有找到医生用户！删除失败！");
            return ResultData.fail("没有找到该用户！删除失败！");
        }
        //删除医生的健康检测任务
        checkTaskService.delDoctorCheckTask(doctor.getId());
        //删除医生
        doctorService.delDoctor(doctor.getId());
        System.out.println("删除医生用户：" + doctor.getId());
        return ResultData.success(true, "用户删除成功！");
    }

    @UserLoginToken
    @PostMapping("/updateDoctor")
    public ResultData<Doctor> update(@RequestBody Doctor doctor) {
        //判断医生是否存在
        if (doctorService.findDoctorById(doctor.getId()) == null) {
            System.out.println("没有找到医生用户！修改失败！");
            return ResultData.fail("没有找到该用户！修改失败！");
        }
        //判断是否有使用该手机号的用户
        if (doctorService.findDoctor(doctor.getPhone()) != null && doctorService.findDoctor(doctor.getPhone()).getId() != doctor.getId()) {
            System.out.println("手机号已被使用！修改失败！");
            return ResultData.fail("手机号已被使用！修改失败！");
        }
        doctorService.updateDoctor(doctor);
        System.out.println("修改医生用户：" + doctor.getPhone());
        return ResultData.success(doctor, "用户修改成功！");
    }

    @UserLoginToken
    @GetMapping("/findAll")
    public ResultData<List<Doctor>> findAll(
            @RequestParam(required = false) String doctorName,
            @RequestParam(required = false) String phone,
            @RequestParam(required = false) Integer communityId
    ) {
        List<Doctor> doctors = doctorService.findAll(doctorName, phone, communityId);
        System.out.println("查询医生用户");
        return ResultData.success(doctors, "用户查询成功！");
    }

    @UserLoginToken
    @GetMapping("/findDoctorById")
    public ResultData<Doctor> findDoctorById(int doctorId) {
        Doctor doctor = doctorService.findDoctorById(doctorId);
        if (doctor == null) {
            System.out.println("没有找到该医生用户！");
            return ResultData.fail("没有找到该用户！");
        }
        System.out.println("用户信息查询成功！");
        return ResultData.success(doctor, "用户信息查询成功！");
    }

    @UserLoginToken
    @GetMapping("/findDoctorByToken")
    public ResultData<Doctor> findDoctorByToken(String token) {
        int id = TokenUtil.getTokenUserId(token);
        Doctor doctor = doctorService.findDoctorById(id);
        if (doctor == null) {
            System.out.println("没有找到该医生用户！");
            return ResultData.fail("没有找到该用户！");
        }
        System.out.println("用户信息查询成功！");
        return ResultData.success(doctor, "用户信息查询成功！");
    }

    @UserLoginToken
    @GetMapping("/getAllCheckTask")
    public ResultData<List<CheckTask>> getAllCheckTask() {
        List<CheckTask> checkTasks = checkTaskService.getAllCheckTask();
        System.out.println("查询所有检查任务");
        return ResultData.success(checkTasks, "检查任务查询成功！");
    }

    @UserLoginToken
    @GetMapping("/getDoctorCheckTask")
    public ResultData<List<CheckTask>> getDoctorCheckTask(@RequestParam int doctorId, @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date date, @RequestParam(required = false) String elderName) {
        //判断医生是否存在
        if (doctorService.findDoctorById(doctorId) == null) {
            System.out.println("没有找到医生用户！查询失败！");
            return ResultData.fail("没有找到该用户！查询失败！");
        }
        List<CheckTask> checkTasks = new ArrayList<>();
        if (date == null && (elderName == null || elderName.isEmpty())) {
            checkTasks = checkTaskService.getDoctorCheckTask(doctorId, null, 0);
        } else {
            List<Elder> elder = elderService.findAll(elderName, null, null);
            for (Elder value : elder) {
                checkTasks.addAll(checkTaskService.getDoctorCheckTask(doctorId, date, value.getId()));
            }
        }
        System.out.println("查询医生所有检查任务");
        //把checkTasks按日期倒着排序
        checkTasks.sort((o1, o2) -> o2.getDate().compareTo(o1.getDate()));
        return ResultData.success(checkTasks, "检查任务查询成功！");
    }

    @UserLoginToken
    @DeleteMapping("/delDoctorCheckTask")
    public ResultData<Boolean> delDoctorCheckTask(int id) {
        //判断检查任务是否存在
        if (checkTaskService.getCheckTaskById(id) == null) {
            System.out.println("没有找到医生检查任务！删除失败！");
            return ResultData.fail("没有找到该检查任务！删除失败！");
        }
        checkTaskService.delCheckTask(id);
        System.out.println("删除医生检查任务");
        return ResultData.success(true, "检查任务删除成功！");
    }

    @UserLoginToken
    @PostMapping("/updateDoctorCheckTask")
    public ResultData<Boolean> updateDoctorCheckTask(@RequestBody CheckTask checkTask) {
        //判断检查任务是否存在
        if (checkTaskService.getCheckTaskById(checkTask.getId()) == null) {
            System.out.println("没有找到医生检查任务！修改失败！");
            return ResultData.fail("没有找到该检查任务！修改失败！");
        }
        //查看老人和医生是否在同一个社区
        Doctor doctor = doctorService.findDoctorById(checkTask.getDoctorId());
        Elder elder = elderService.findElderById(checkTask.getElderId());
        if (doctor.getCommunityId() != elder.getCommunityId()) {
            System.out.println("老人和医生不在同一个社区！修改失败！");
            return ResultData.fail("老人和医生不在同一个社区！修改失败！");
        }
        checkTaskService.updateDoctorCheckTask(checkTask);
        System.out.println("修改医生检查任务");
        return ResultData.success(true, "检查任务修改成功！");
    }

}
